package com.aiju.cash.ui.main;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import com.aiju.base.activity.BaseActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.login.LoginActivity;
import com.aiju.cash.ui.login.manager.LoginManager;
import com.aiju.utils.PreferencesUtils;
import com.aiju.utils.tts.BaiduTTSManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * @author: 佐为
 * @date: 2016-12-20 14:42
 * @version: V1.0
 * @Description: 启动页
 */
public class SplashActivity extends BaseActivity {
    boolean isFirstIn = false;

    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    private static final int GO_LOGIN = 1002;
    private static final long SPLASH_DELAY_MILLIS = 2500;

    private static final String SHAREDPREFERENCES_NAME = "first_pref";
    private String name, password;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    goHome();
                    break;
                case GO_GUIDE:
                    goGuide();
                    break;
                case GO_LOGIN:
                    SSOLogin();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        init();
    }

    @Override
    protected void setStatusBar() {
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setTintColor(getTitleColor());
        tintManager.setStatusBarAlpha(0);
    }

    private void init() {
        BaiduTTSManager.getInstance().init(this);
        SharedPreferences preferences = getSharedPreferences(SHAREDPREFERENCES_NAME, MODE_PRIVATE);
        isFirstIn = preferences.getBoolean("isFirstIn", true);
        if (!isFirstIn) {
            name = PreferencesUtils.getString(this, "name");
            password = PreferencesUtils.getString(this, "password");
            if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
                mHandler.sendEmptyMessageDelayed(GO_LOGIN, SPLASH_DELAY_MILLIS);
            } else {
                mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
            }
        } else {
            mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
        }
    }

    private void SSOLogin() {
        //startActivity(MemberInfoActivity.class);
        new LoginManager(SplashActivity.this, mDialogHelper).loginSSO(name, password);
    }

    private void goHome() {
        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private void goGuide() {
        Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
        startActivity(intent);
        finish();
    }
}